package com.zhangdi.dwtools.io;

import java.io.InputStream;

public class ClassUtils {
    private static final ClassUtils.ClassLoaderAccessor THREAD_CL_ACCESSOR = new ClassUtils.ExceptionIgnoringAccessor() {
        protected ClassLoader doGetClassLoader() {
            return Thread.currentThread().getContextClassLoader();
        }
    };
    private static final ClassUtils.ClassLoaderAccessor CLASS_CL_ACCESSOR = new ClassUtils.ExceptionIgnoringAccessor() {
        protected ClassLoader doGetClassLoader() {
            return ClassUtils.class.getClassLoader();
        }
    };
    private static final ClassUtils.ClassLoaderAccessor SYSTEM_CL_ACCESSOR = new ClassUtils.ExceptionIgnoringAccessor() {
        protected ClassLoader doGetClassLoader() {
            return ClassLoader.getSystemClassLoader();
        }
    };

    public ClassUtils() {
    }

    public static InputStream getResourceAsStream(String name) {
        InputStream is = THREAD_CL_ACCESSOR.getResourceStream(name);
        if (is == null) {
            is = CLASS_CL_ACCESSOR.getResourceStream(name);
        }

        if (is == null) {
            is = SYSTEM_CL_ACCESSOR.getResourceStream(name);
        }

        return is;
    }

    private abstract static class ExceptionIgnoringAccessor implements ClassUtils.ClassLoaderAccessor {
        private ExceptionIgnoringAccessor() {
        }

        public Class loadClass(String fqcn) {
            Class clazz = null;
            ClassLoader cl = this.getClassLoader();
            if (cl != null) {
                try {
                    clazz = cl.loadClass(fqcn);
                } catch (ClassNotFoundException var5) {
                    var5.printStackTrace();
                }
            }

            return clazz;
        }

        public InputStream getResourceStream(String name) {
            InputStream is = null;
            ClassLoader cl = this.getClassLoader();
            if (cl != null) {
                is = cl.getResourceAsStream(name);
            }

            return is;
        }

        protected final ClassLoader getClassLoader() {
            try {
                return this.doGetClassLoader();
            } catch (Throwable var2) {
                var2.printStackTrace();
                return null;
            }
        }

        protected abstract ClassLoader doGetClassLoader() throws Throwable;
    }

    private interface ClassLoaderAccessor {
        Class loadClass(String var1);

        InputStream getResourceStream(String var1);
    }
}